package org.example.leetCode;

import java.util.HashMap;

/**
 * @author Vader
 * @date 2021/11/5
 **/
public class LongestSubsequence {
    public static void main(String[] args) {
        int[] arr = {1, 5, 7, 8, 5, 3, 4, 2, 1};
        int difference = -2;
        System.out.println(longestSubsequence(arr, difference));
    }

    public static int longestSubsequence(int[] arr, int difference) {
        if (arr.length < 1) {
            return 0;
        } else if (arr.length < 2) {
            return 1;
        }
        int max = 1;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int j : arr) {
            Integer val = map.get(j - difference);
            if (val != null) {
                map.put(j, val + 1);
                max = Math.max(max, val + 1);
            } else {
                map.put(j, 1);
            }
        }
        return max;
    }
}
